import com.mt.components.consts.Exception;
import com.mt.components.consts.PermissionName;
import com.mt.components.consts.SysConst;
import com.mt.components.log.LogFactory;
import com.mt.components.log.Logger;
import com.mt.components.utils.AlertFactory;
import com.mt.components.utils.RemoteFactory;
import com.mt.system.domain.Permission;
import com.mt.system.domain.Role;
import com.mt.system.view.RoleView;
import com.mt.system.view.role.SaveRolePermissionView;
import com.mt.system.view.role.SaveRoleView;

import flash.events.Event;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.core.FlexGlobals;
import mx.events.CloseEvent;
import mx.events.ItemClickEvent;
import mx.managers.PopUpManager;
import mx.rpc.Fault;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;

/**      
 * 文件名称：RoleViewAs.as 
 * 文件描述：
 * 创建人：马腾
 * 创建时间：Nov 23, 2012 10:26:32 PM
 * 修改人：马腾
 * 修改时间：Nov 23, 2012 10:26:32 PM
 */

private static var logger:Logger = LogFactory.getLogger(RoleView);

[Bindable]private var roles:ArrayCollection;

public function submit():void {
	var remote:RemoteObject = RemoteFactory.getInstance().getRemote("RoleService");
	remote.addEventListener(ResultEvent.RESULT , resultData);
	remote.getAllRoles();
}

protected function resultData(event:ResultEvent):void {
	roles = event.result as ArrayCollection;
}

protected function click(event:ItemClickEvent):void {
	var permission:Permission = event.item as Permission;
	if(permission.mark == PermissionName.ROLE_ADD){
		addRole();
	}else if(permission.mark == PermissionName.ROLE_MOD){
		updateRole();
	}else if(permission.mark == PermissionName.ROLE_ASSIGN_PER){
		updateRolePermission();
	}else if(permission.mark == PermissionName.ROLE_DEL){
		deleteRole();
	}else if(permission.mark == PermissionName.ROLE_REF){
		submit();
	}
}

private function deleteRole():void {
	var selectItems:Array = grid.selectedItems;
	if(!selectItems || selectItems.length == 0){
		msg.text = "请选中记录进行操作！";
		return;
	}
	AlertFactory.getInstance().confirmAlert(confirmDelete);
}

private function confirmDelete(event:CloseEvent):void {
	if(event.detail == Alert.YES){
		var remote:RemoteObject = RemoteFactory.getInstance().getRemote("RoleService" , PermissionName.ROLE_DEL);
		remote.addEventListener(ResultEvent.RESULT , function (e:ResultEvent):void {
			msg.text = "删除成功！";
			submit();
		});
		remote.addEventListener(FaultEvent.FAULT , function (e:FaultEvent):void {
			if(e.message.toString().indexOf(Exception.FIND_USER_BIND_ROLE) != -1){
				msg.text = "发现有用户绑定该角色,无法进行删除！";
			}else{
				msg.text = "删除失败!";
			}
		});
		remote.deleteRole(grid.selectedItem);
	}
}

private function addRole():void {
	var saveRoleView:SaveRoleView = new SaveRoleView();
	saveRoleView.title = "增加角色";
	PopUpManager.addPopUp(saveRoleView , FlexGlobals.topLevelApplication.vk , true);
	PopUpManager.centerPopUp(saveRoleView);
	saveRoleView.addEventListener(SysConst.SAVE_SUCCESS , saveSuccess);
}

private function updateRolePermission():void {
	if(!grid.selectedItem){
		msg.text = "请选中记录进行操作！";
		return;
	}
	var saveRoleView:SaveRolePermissionView = new SaveRolePermissionView();
	saveRoleView.title = "角色权限分配";
	saveRoleView.role = grid.selectedItem as Role;
	PopUpManager.addPopUp(saveRoleView , FlexGlobals.topLevelApplication.vk , true);
	PopUpManager.centerPopUp(saveRoleView);
	saveRoleView.addEventListener(SysConst.SAVE_SUCCESS , saveSuccess);
}

private function updateRole():void {
	if(!grid.selectedItem){
		msg.text = "请选中记录进行操作！";
		return;
	}
	var saveRoleView:SaveRoleView = new SaveRoleView();
	saveRoleView.title = "修改角色";
	saveRoleView.role = grid.selectedItem as Role;
	PopUpManager.addPopUp(saveRoleView , FlexGlobals.topLevelApplication.vk , true);
	PopUpManager.centerPopUp(saveRoleView);
	saveRoleView.addEventListener(SysConst.SAVE_SUCCESS , saveSuccess);
}

/**
 * 保存角色成功后,刷新页面
 * @param event
 */
protected function saveSuccess(event:Event):void {
	submit();
}
